package com.eascs.dubbo.api.internal;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.api.internal.InternalManageInfoService;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.dao.NsEditDetailDao;
import com.eascs.crm.nsEditDetail.entity.CustomerShopInfoEntityNew;
import com.eascs.crm.nsEditDetail.entity.ManagementTdInfoEntity;
import com.eascs.crm.nsEditDetail.entity.O2oMainShopInfoEntity;
import com.eascs.crm.nsEditDetail.service.CustomerShopInfoService;
import com.eascs.crm.nsEditDetail.service.IMainShopInfoService;
import com.eascs.crm.nsEditDetail.service.ManagementTdInfoService;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

/**
 * 内审经营信息接口
 * 
 * @author user
 *
 */
@Service
public class InternalManageInfoServiceImpl implements InternalManageInfoService {

	@Autowired
	NsEditDetailDao nsDao;

	@Autowired
	IMainShopInfoService mainShopInfoService;

	@Autowired
	ManagementTdInfoService managementTdInfoService;

	@Autowired
	CustomerShopInfoService customerShopInfoService;

	/**
	 * 根据贷款id查询客户门店id
	 * 
	 * @param loanapplyNo
	 * @return
	 */
	@Override
	public List<String> queryCustomerShopId(String loanapplyNo) {
		return nsDao.findCustomerShopId(loanapplyNo);
	}

	/**
	 * 查询客户门店详情
	 * 
	 * @param customerShopId
	 * @return
	 */
	@Override
	public CustomerShopInfoEntityNew queryCustomerShopDetail(String customerShopId) {
		return nsDao.queryByIdnew(customerShopId);
	}

	@Override
	public String saveManageMainInfo(O2oMainShopInfoEntity o2oMainShopInfoEntity) {
		String result = null;
		O2oMainShopInfoEntity entity = mainShopInfoService.getByLoanNo(o2oMainShopInfoEntity.getLoanapplyNo());
		if (entity == null) {
			createO2oMainShopInfo(o2oMainShopInfoEntity);
			boolean flag = mainShopInfoService.insert(o2oMainShopInfoEntity);// 新增
			if (!flag) {
				result = "添加经营主体信息失败";
			}
		} else {
			if (StringUtil.isBlank(o2oMainShopInfoEntity.getId())) {
				result = "更新经营主体信息失败:记录不存在";
			} else {
				O2oMainShopInfoEntity info = mainShopInfoService.getEntityById(o2oMainShopInfoEntity.getId());
				o2oMainShopInfoEntity = modifyO2oMainShopInfo(info, o2oMainShopInfoEntity);
				boolean flag = mainShopInfoService.update(o2oMainShopInfoEntity);// 修改(form表单字段多的就反过来修改form表单)
				if (!flag) {
					result = "修改经营主体信息失败";
				}
			}
		}
		return result;
	}

	@Override
	public String addManageHistory(ManagementTdInfoEntity managementTdInfoEntity) {
		String result = null;
		User user = UserHolder.getUser();
		managementTdInfoEntity.setCreateDate(new Date());
		managementTdInfoEntity.setModifyDate(new Date());
		managementTdInfoEntity.setCreateId(user.getNo());
		managementTdInfoEntity.setModifyId(user.getNo());
		managementTdInfoEntity.setCreator(user.getCname());
		managementTdInfoEntity.setModifier(user.getCname());

		boolean flag = managementTdInfoService.insert(managementTdInfoEntity);
		if (!flag) {
			result = "保存年度经营情况失败";
		}
		return result;
	}

	@Override
	public ManagementTdInfoEntity queyManageHistoryByid(String id) {
		return managementTdInfoService.queryById(id);
	}

	@Override
	public String updateManageHistory(ManagementTdInfoEntity managementTdInfoEntity) {
		String result = null;
		if (StringUtil.isBlank(managementTdInfoEntity.getId())) {
			result = "更新年度经营情况失败:记录不存在";
		}
		User user = UserHolder.getUser();
		managementTdInfoEntity.setModifyDate(new Date());
		managementTdInfoEntity.setModifyId(user.getNo());
		managementTdInfoEntity.setModifier(user.getCname());
		boolean flag = managementTdInfoService.update(managementTdInfoEntity);
		if (!flag) {
			result = "更新年度经营情况失败";
		}
		return result;
	}

	@Override
	public String deleteManageHistory(String id) {
		String result = null;
		boolean flag = managementTdInfoService.delete(id);
		if (!flag) {
			result = "删除年度经营情况失败";
		}
		return result;
	}

	@Override
	public String addShopInfo(CustomerShopInfoEntityNew customerShopInfoEntityNew){
		String result = null;
		User user = UserHolder.getUser();
		customerShopInfoEntityNew.setCreateDate(new Date());
		customerShopInfoEntityNew.setModifyDate(new Date());
		customerShopInfoEntityNew.setCreateId(user.getNo());
		customerShopInfoEntityNew.setModifyId(user.getNo());
		customerShopInfoEntityNew.setCreator(user.getCname());
		customerShopInfoEntityNew.setModifier(user.getCname());
		boolean flag = customerShopInfoService.insertnew(customerShopInfoEntityNew);
		if (!flag) {
			result = "添加门店信息失败";
		}
		return result;
	}

	@Override
	public String updateShopInfo(CustomerShopInfoEntityNew customerShopInfoEntityNew){
		String result = null;
		if (StringUtil.isBlank(customerShopInfoEntityNew.getCustomerShopId())) {
			result = "更新门店信息失败:记录不存在";
		}
		User user = UserHolder.getUser();
		customerShopInfoEntityNew.setModifyDate(new Date());
		customerShopInfoEntityNew.setModifyId(user.getNo());
		customerShopInfoEntityNew.setModifier(user.getCname());
		boolean flag = customerShopInfoService.updatenew(customerShopInfoEntityNew);
		if (!flag) {
			result = "修改门店信息失败";
		}
		return result;
	}

	@Override
	public String deleteShopInfo(String customerShopId) {
		String result = null;
		boolean flag = customerShopInfoService.delete(customerShopId);
		if (!flag) {
			result = "删除门店信息失败";
		}
		return result;
	}

	// utils---------------------------------------------------------------------------------------------------------------------------
	private void createO2oMainShopInfo(O2oMainShopInfoEntity info) {
		User user = UserHolder.getUser();
		info.setCreateDate(new Date());
		info.setModifyDate(new Date());
		info.setCreateId(user.getNo());
		info.setModifyId(user.getNo());
		info.setCreator(user.getCname());
		info.setModifier(user.getCname());
	}

	private O2oMainShopInfoEntity modifyO2oMainShopInfo(O2oMainShopInfoEntity info, O2oMainShopInfoEntity form) {
		User user = UserHolder.getUser();
		form.setModifyDate(new Date());
		form.setModifyId(user.getNo());
		form.setModifier(user.getCname());
		form.setCreateId(info.getCreateId());
		form.setCreateDate(info.getCreateDate());
		form.setCreator(info.getCreator());
		return form;
	}
}
